minetest.register_node('stations:printing_press', {
   description = 'Printing Press',
   drawtype = 'mesh',
   mesh = 'stations_printing_press.obj',
   tiles = {'stations_printing_press.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {
         {-.5, .4, -.5, 0, 1, .5}, --Upright
         {-.5, -.5, -.5, 1, .4, .5}, --base
         }
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1, .4, .5}, --base
      },
   groups = {oddly_breakable_by_hand=3, choppy=1},
   after_place_node = function(pos, placer, itemstack)
      if not epic.space_to_top_and_side(pos) then
         minetest.remove_node(pos)
         return itemstack
      else
         local meta = minetest.get_meta(pos)
         local inv = meta:get_inventory()
         inv:set_size('master', 1)
         inv:set_size('input', 1)
         inv:set_size('output', 1)
         meta:set_string('infotext', 'printing press')
         meta:set_string('owner' , placer:get_player_name())
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      epic.remove_side_node(pos, oldnode)
      epic.remove_top_node(pos)
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      if listname == 'output' then
         return 0
      elseif listname == 'master' then
         local meta = minetest.get_meta(pos)
         local name = player:get_player_name()
         local owner = meta:get_string('owner')
         if name == owner then
            if stack:get_name() == ('default:book_written') then
               return 1
            else
               return 0
            end
         else
            return 0
         end
      elseif listname == 'input' then
         if stack:get_name() == ('default:book') then
            return 1
         else
            return 0
         end
      end
   end,
   allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
      return 0
   end,
   on_rightclick = function(pos, node, player, itemstack, pointed_thing)
      local meta=minetest.get_meta(pos)
      local name = player:get_player_name()
      local owner = meta:get_string('owner')
      local spos = pos.x..','..pos.y..','..pos.z
      local gui = 'size[8,7;]'..
         'label[5,0.25;Master Copy:]'..
         'list[nodemeta:'..spos..';master;4,0;1,1]'..
         'label[1.5,1.25;Empty Book.]'..
         'list[nodemeta:'..spos..';input;.5,1;1,1;]'..
         'label[1.5,2.25;Printed Copy.]'..
         'list[nodemeta:'..spos..';output;.5,2;1,1;]'..
         'button[4.5,1.5;2,1;copy;Print a copy!]'..
         'list[current_player;main;0,3;8,4;]'
         meta:set_string('formspec', gui)
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)
      if fields ['copy'] then
         local meta = minetest.get_meta(pos)
         local inv = meta:get_inventory()
         local master_stack = inv:get_stack('master', 1)
         local input_stack = inv:get_stack('input', 1)
         local master_book = master_stack:get_name()
         local empty_book = input_stack:get_name()
         if master_book == 'default:book_written' then
            if empty_book == 'default:book' then
               inv:add_item('output', 'default:book_written')
               input_stack:take_item(1)
               local output = inv:get_stack('output', 1)
               local copymeta = master_stack:get_meta():to_table()
               output:get_meta():from_table(copymeta)
               inv:set_stack('output', 1, output)
               inv:set_stack('input', 1, input_stack)
            end
         end
      end
   end,
   allow_metadata_inventory_take = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local name = player:get_player_name()
      local owner = meta:get_string('owner')
         if listname == 'master' then
            if name == owner then
               return 1
            else
               return 0
            end
         elseif listname == 'input' then
            return 1
         elseif listname == 'output' then
            return 1
         end
   end,
})
